﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Managers {
    import Engine.Classes.*;
    import flash.events.*;
    import flash.utils.*;
    import flash.media.*;
    import flash.net.*;

    public class SoundManager {

        protected static var m_sounds:Dictionary = new Dictionary();
        protected static var m_activeChannels:Array = [];
        protected static var m_isMuted:Boolean = false;

        public static function addSound(_arg1:String, _arg2:String, _arg3:Boolean=true, _arg4:Function=null):Sound{
            var sound:* = null;
            var onLoadCompleteWrapper:* = null;
            var id:* = _arg1;
            var url:* = _arg2;
            var playOnLoad:Boolean = _arg3;
            var onLoadComplete = _arg4;
            sound = m_sounds[id];
            if (sound == null){
                sound = new Sound();
                sound.addEventListener(Event.COMPLETE, onComplete);
                if (playOnLoad){
                    sound.addEventListener(Event.COMPLETE, onPlayOnComplete);
                };
                if (onLoadComplete != null){
                    onLoadCompleteWrapper = function (_arg1:Event):void{
                        onLoadComplete(_arg1);
                        sound.removeEventListener(Event.COMPLETE, onLoadCompleteWrapper);
                    };
                    sound.addEventListener(Event.COMPLETE, onLoadCompleteWrapper);
                };
                sound.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
                sound.load(new URLRequest(AssetUrlManager.instance.lookUpUrl(url)));
                m_sounds[id] = sound;
                GlobalEngine.log("Sound", ((("Add sound, id: " + id) + ", url: ") + url));
            };
            return (sound);
        }
        protected static function onComplete(_arg1:Event):void{
            var _local2:Sound = (_arg1.target as Sound);
            GlobalEngine.log("Sound", ("Sound loaded: " + _local2.url));
        }
        protected static function onIOError(_arg1:Event):void{
            var _local2:Sound = (_arg1.target as Sound);
            ErrorManager.addError(("Error loading sound: " + _local2.url), ErrorManager.ERROR_LOAD_IO_ERROR);
        }
        public static function playSound(_arg1:String, _arg2:Number=0, _arg3:int=0, _arg4:SoundTransform=null, _arg5:Boolean=false, _arg6:Boolean=false):SoundObject{
            var sound:* = null;
            var id:* = _arg1;
            var startTime:Number = _arg2;
            var loops:int = _arg3;
            var transform = _arg4;
            var isMusic:Boolean = _arg5;
            var repeatLoop:Boolean = _arg6;
            var result:* = null;
            try {
                sound = getSoundById(id);
                GlobalEngine.log("Sound", (((("Play Sound: " + id) + " (muted: ") + isMuted()) + ")"));
                if (((((sound) && ((sound.bytesTotal > 0)))) && ((sound.bytesLoaded == sound.bytesTotal)))){
                    result = conditionallyPlaySound(sound, startTime, loops, transform, isMusic, repeatLoop);
                };
            } catch(e:Error) {
                GlobalEngine.log("Sound", (("Error: Can't play sound (" + id) + ")"));
            };
            return (result);
        }
        public static function stopSound(_arg1:String):void{
            var _local3:int;
            var _local4:SoundObject;
            var _local2:Sound = getSoundById(_arg1);
            if (_local2 != null){
                _local3 = 0;
                while (_local3 < m_activeChannels.length) {
                    _local4 = (m_activeChannels[_local3] as SoundObject);
                    if ((((((_local2.url == null)) && ((_local2 == _local4.sound)))) || (((!((_local2.url == null))) && ((_local4.sound.url == _local2.url)))))){
                        _local4.stop();
                    };
                    _local3++;
                };
            };
        }
        public static function stopAllMusic():void{
            var _local2:SoundObject;
            var _local1:int;
            while (_local1 < m_activeChannels.length) {
                _local2 = (m_activeChannels[_local1] as SoundObject);
                if (((_local2) && ((_local2.isMusic == true)))){
                    _local2.stop();
                };
                _local1++;
            };
        }
        public static function stopAllSounds():void{
            var _local2:SoundObject;
            var _local1:int;
            while (_local1 < m_activeChannels.length) {
                _local2 = (m_activeChannels[_local1] as SoundObject);
                if (((_local2) && ((_local2.isMusic == false)))){
                    _local2.stop();
                };
                _local1++;
            };
            m_activeChannels = [];
        }
        private static function conditionallyPlaySound(_arg1:Sound, _arg2:Number=0, _arg3:int=0, _arg4:SoundTransform=null, _arg5:Boolean=false, _arg6:Boolean=false):SoundObject{
            var _local8:SoundChannel;
            var _local7:SoundObject;
            if (_arg5){
                SoundManager.stopAllMusic();
            };
            if ((((_arg5 == true)) || ((isMuted() == false)))){
                if (_arg6){
                    _local8 = _arg1.play((_arg2 % _arg1.length), 0, _arg4);
                } else {
                    _local8 = _arg1.play((_arg2 % _arg1.length), _arg3, _arg4);
                };
                if (_local8){
                    _local7 = new SoundObject(_arg1, _local8, _arg3, _arg4);
                    _local7.isMusic = _arg5;
                    m_activeChannels.push(_local7);
                    _arg1.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
                };
            };
            return (_local7);
        }
        private static function onSoundComplete(_arg1:Event):void{
            var _local4:SoundObject;
            var _local2:Sound = (_arg1.target as Sound);
            var _local3:int;
            while (_local3 < m_activeChannels.length) {
                _local4 = (m_activeChannels[_local3] as SoundObject);
                if ((((((_local2.url == null)) && ((_local2 == _local4.sound)))) || (((!((_local2.url == null))) && ((_local4.sound.url == _local2.url)))))){
                    if (_local4.canReloop()){
                        _local4.stop(false);
                        _local2.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
                        conditionallyPlaySound(_local2, 0, _local4.loops, new SoundTransform(_local4.volume));
                    };
                    m_activeChannels.splice(m_activeChannels.indexOf(_local4), 1);
                    break;
                };
                _local3++;
            };
        }
        private static function onPlayOnComplete(_arg1:Event):void{
            var _local2:Sound = (_arg1.target as Sound);
            _local2.removeEventListener(Event.COMPLETE, onPlayOnComplete);
            if (_local2){
                conditionallyPlaySound(_local2);
            };
        }
        public static function getSoundById(_arg1:String):Sound{
            return ((m_sounds[_arg1] as Sound));
        }
        public static function getSoundObjectById(_arg1:String):SoundObject{
            var _local4:int;
            var _local5:SoundObject;
            var _local2:Sound = getSoundById(_arg1);
            var _local3:SoundObject;
            if (_local2 != null){
                _local4 = 0;
                while (_local4 < m_activeChannels.length) {
                    _local5 = (m_activeChannels[_local4] as SoundObject);
                    if ((((((_local2.url == null)) && ((_local2 == _local5.sound)))) || (((!((_local2.url == null))) && ((_local5.sound.url == _local2.url)))))){
                        _local3 = _local5;
                        break;
                    };
                    _local4++;
                };
            };
            return (_local3);
        }
        public static function getPlayTimeElapsed(_arg1:String):Number{
            var _local4:int;
            var _local5:SoundObject;
            var _local2:Sound = getSoundById(_arg1);
            var _local3:Number = 0;
            if (_local2 != null){
                _local4 = 0;
                while (_local4 < m_activeChannels.length) {
                    _local5 = (m_activeChannels[_local4] as SoundObject);
                    if ((((((_local2.url == null)) && ((_local2 == _local5.sound)))) || (((!((_local2.url == null))) && ((_local5.sound.url == _local2.url)))))){
                        _local3 = _local5.position;
                        break;
                    };
                    _local4++;
                };
            };
            return (_local3);
        }
        public static function mute():void{
            var _local2:SoundObject;
            m_isMuted = true;
            var _local1:int;
            while (_local1 < m_activeChannels.length) {
                _local2 = (m_activeChannels[_local1] as SoundObject);
                if (((_local2) && ((_local2.isMusic == false)))){
                    _local2.volume = 0;
                };
                _local1++;
            };
        }
        public static function unmute():void{
            var _local2:SoundObject;
            m_isMuted = false;
            var _local1:int;
            while (_local1 < m_activeChannels.length) {
                _local2 = (m_activeChannels[_local1] as SoundObject);
                if (((_local2) && ((_local2.isMusic == false)))){
                    _local2.volume = 1;
                };
                _local1++;
            };
        }
        public static function toggleMute():void{
            if (isMuted()){
                unmute();
            } else {
                mute();
            };
        }
        public static function isMuted():Boolean{
            return (m_isMuted);
        }
        public static function chooseAndPlaySound(_arg1:Array):void{
            var _local3:Number;
            var _local2:String;
            if (_arg1.length == 1){
                _local2 = (_arg1[0] as String);
            } else {
                if (_arg1.length > 0){
                    _local3 = Math.floor((Math.random() * _arg1.length));
                    _local2 = (_arg1[_local3] as String);
                };
            };
            if (_local2 != null){
                playSound(_local2);
            };
        }

    }
}//package Engine.Managers 
